package com.hualude.roadsign.service.impl;

import com.hualude.roadsign.dto.*;
import com.hualude.roadsign.repository.CountRepository;
import com.hualude.roadsign.repository.RoadLineFaultCountRepository;
import com.hualude.roadsign.service.RoadLineFaultCountService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by syk on 2017/11/21.
 */
@Service
public class RoadLineFaultCountServiceImpl implements RoadLineFaultCountService {

    @Resource
    private RoadLineFaultCountRepository roadLineFaultCountRepository;
    @Resource
    private CountRepository countRepository;

    @Override
    public List<DealTalkCountsDto> getDealReportExpensesCount(Integer year, Integer orgId, Integer userId) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        String orgIds = orgId.toString();
        //if (orgId == -1)
        if (authorityDto.getActor() == 7 || authorityDto.getActor() == 1) {
            orgIds = authorityDto.getOrgIds();
        }
        List<String> listDealCounts = countRepository.getRoadLineOrgYearDealTimes(year, orgIds);
        List<String> listReportCounts = countRepository.getRoadLineOrgyearTalkTimes(year, orgIds);
        List<MaintenanceDto> listExpenseCounts = countRepository.getRoadLineOrgyearSpendMoney(year, orgIds);
        List<DealTalkCountsDto> dealCountDtos = new ArrayList<>(listDealCounts.size());
        List<DealTalkCountsDto> TalkCountDtos = new ArrayList<>(listReportCounts.size());
        List<DealTalkCountsDto> spendMoneyDtos = new ArrayList<>(listExpenseCounts.size());
        for (String time : listDealCounts) {
            dealCountDtos.add(new DealTalkCountsDto(1, time));
        }
        for (String time : listReportCounts) {
            TalkCountDtos.add(new DealTalkCountsDto(0, time));
        }

        for (int i = 0; i < listExpenseCounts.size(); i++) {
            double repairAmount = 0;
            if (listExpenseCounts.get(i).getRepairAmount() != null) {
                repairAmount = listExpenseCounts.get(i).getRepairAmount();
                BigDecimal b = new BigDecimal(repairAmount);
                repairAmount = b.setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
            }
            spendMoneyDtos.add(new DealTalkCountsDto(2, listExpenseCounts.get(i).getMaintenanceTime(), repairAmount));
        }
        dealCountDtos.addAll(TalkCountDtos);
        dealCountDtos.addAll(spendMoneyDtos);
        return dealCountDtos;
    }

    //得到用户公司及其子公司IDs，用户最终角色
    public AuthorityDto getActorAndOrgIds(AuthorityDto authorityDto) {
        try {
            String actorIds = countRepository.getactorIds(authorityDto.getUserId());//获取该用户的所有角色
            String orgIds = authorityDto.getOrgId().toString();
            if (orgIds.equals("-1")) {
                orgIds = countRepository.getRealOrgIds(authorityDto.getUserId()).toString();
            }
            Integer actor = Integer.parseInt(actorIds.split(",")[0]);//用户最终角色 1系统>7主管>其它
            Integer isManager = actorIds.indexOf("1");//系统管理员
            Integer isChief = actorIds.indexOf("7");//主管管理员
            if (isManager >= 0) {
                actor = 1;
                String ChildOrgIds = countRepository.getOrgIds();
                if (!ChildOrgIds.equals("")) {
                    orgIds = ChildOrgIds;
                }
            } else if (isChief >= 0) {
                actor = 7;
                String ChildOrgIds = countRepository.getChildOrgIds(Integer.parseInt(orgIds));
                if (!ChildOrgIds.equals("")) {
                    orgIds += "," + ChildOrgIds;
                }
            }
            authorityDto.setActor(actor);
            authorityDto.setOrgIds(orgIds);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return authorityDto;
    }

    @Override
    public DTResponseDto getFault4DT(Integer userId, Integer orgId, DTRequestDto dtRequestDto) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        Integer count = roadLineFaultCountRepository.getFaultCount4DT(dtRequestDto, authorityDto.getActor(), authorityDto.getOrgIds());
        List<LineFaultDto> dto4DTs = roadLineFaultCountRepository.getFault4DT(dtRequestDto, authorityDto.getActor(), authorityDto.getOrgIds());
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        return dtResponseDto;
    }

    @Override
    public DTResponseDto getDevice4DT(Integer userId, Integer orgId, DTRequestDto dtRequestDto) throws Exception {
        AuthorityDto authorityDto = new AuthorityDto(userId, orgId);
        authorityDto = getActorAndOrgIds(authorityDto);
        Integer count = roadLineFaultCountRepository.getDeviceCount4DT(dtRequestDto, authorityDto.getActor(), authorityDto.getOrgIds(), authorityDto.getUserId());
        List<LineFaultDto> dto4DTs = roadLineFaultCountRepository.getDevice4DT(dtRequestDto, authorityDto.getActor(), authorityDto.getOrgIds(), authorityDto.getUserId());
        DTResponseDto dtResponseDto = new DTResponseDto(dtRequestDto.getDraw(), count, count, dto4DTs);
        return dtResponseDto;
    }
}
